package com.weayer.service.Impl;

import com.weayer.dto.ResponseResult;
import com.weayer.request.DriverGrabRequest;
import com.weayer.service.GrabService;
import com.weayer.service.OrderInfoService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

/**
 * @description: redis 主从复制
 * @author: Weayer
 **/
@Service("redissonMasterGrabService")
public class RedissonMasterGrabServiceImpl implements GrabService {

    @Autowired
    OrderInfoService orderInfoService;

    @Autowired
    @Qualifier("redissonMasterClient")
    RedissonClient redissonClient;

    @Override
    public ResponseResult grab(DriverGrabRequest driverGrabRequest) {
        String key = driverGrabRequest.getOrderId() + "";
        RLock lock = redissonClient.getLock(key);    // 分布式锁redisson
        lock.lock();
        // 模拟程序执行时间
//        try {
//            TimeUnit.SECONDS.sleep(20);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
        ResponseResult result = orderInfoService.grab(driverGrabRequest);
        lock.unlock();
        return result;
    }
}
